<html xmlns="http://www.w3.org/1999/xhtml" manifest="http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/changingManifest.sjs">
<head>
<title>changing manifest test</title>

<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<script type="text/javascript" src="/tests/dom/tests/mochitest/ajax/offline/offlineTests.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />

<script type="text/javascript">

var gGotChecking = false;
var gGotDownloading = false;

var g1SecUrl =  "http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/changing1Sec.sjs";
var g1HourUrl = "http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/changing1Hour.sjs";

var gCacheContents = null;

function finish()
{
  OfflineTest.teardownAndFinish();
}

function manifestUpdatedAgain()
{
  OfflineTest.ok(gGotChecking, "Should get a checking event on the second update");
  OfflineTest.ok(gGotDownloading, "Should get a downloading event on the second update");

  // The second manifest has no NETWORK entry, but we are still
  // associated with the first version of the manifest, so we should
  // be able to access the whitelisted entry.
  try {
    var xhr = new XMLHttpRequest();
    xhr.open("GET", "onwhitelist.html", false);
    xhr.send();
    OfflineTest.ok(true, "Fetched from the initial cache's whitelist.");
  } catch (e) {
    OfflineTest.ok(false, "Failed to fetch from the initial cache's whitelist.");
  }

  // Get the initial contents of the first two files.
  fetcher = new OfflineCacheContents([g1SecUrl, g1HourUrl]);
  fetcher.fetch(function(contents) {
    // Make sure the contents of the 1-second-expiration file have changed,
    // but that the 1-hour-expiration has not.
    OfflineTest.isnot(gCacheContents[g1SecUrl], contents[g1SecUrl], "1-second expiration should have changed");
    OfflineTest.is(gCacheContents[g1HourUrl], contents[g1HourUrl], "1-hour expiration should not have changed");

    finish();
  });
}

function failAndFinish(e) {
  OfflineTest.ok(false, "Unexpected event: " + e.type);
  finish();
}

function manifestUpdated()
{
  OfflineTest.ok(gGotChecking, "Should get a checking event");
  OfflineTest.ok(gGotDownloading, "Should get a downloading event");

  // Replace this manifest with a new one.
  OfflineTest.setSJSState("changingManifest.sjs", "2");

  // Get the initial contents of the first two files.
  fetcher = new OfflineCacheContents([g1SecUrl, g1HourUrl]);
  fetcher.fetch(function(contents) {
    gCacheContents = contents;

    // Now make sure applicationCache.update() does what we expect.
    applicationCache.onchecking = function() {
      OfflineTest.is(applicationCache.status, applicationCache.CHECKING,
                     "CHECKING state during update");
      gGotChecking = true;
    };
    applicationCache.ondownloading = function() {
      OfflineTest.is(applicationCache.status, applicationCache.DOWNLOADING,
                     "DOWNLOADING state during update");
      gGotDownloading = true;
    };

    applicationCache.onupdateready = OfflineTest.priv(manifestUpdatedAgain);
    applicationCache.onnoupdate = failAndFinish;
    applicationCache.oncached = failAndFinish;

    gGotChecking = false;
    gGotDownloading = false;

    // The changing versions give out a new version each second,
    // make sure it has time to grab a new version, and for the
    // 1-second cache timeout to pass.
    window.setTimeout("applicationCache.update()", 5000);
  });
}

if (OfflineTest.setup()) {
  applicationCache.onerror = failAndFinish;
  applicationCache.onnoupdate = failAndFinish;

  applicationCache.onchecking = function() { gGotChecking = true; };
  applicationCache.ondownloading = function() { gGotDownloading = true; };
  applicationCache.oncached = OfflineTest.priv(manifestUpdated);
}

SimpleTest.waitForExplicitFinish();
SimpleTest.requestFlakyTimeout("untriaged");

</script>

</head>

<body>

</body>
</html>
